package com.dwxt.sport.business.service;

import com.dwxt.common.base.StatusCode;
import com.dwxt.common.constant.VariableConstants;
import com.dwxt.common.util.SnowIdUtil;
import com.dwxt.sport.business.dao.SportsTrainingDetailsMapper;
import com.dwxt.sport.business.dao.SportsTrainingMapper;
import com.dwxt.sport.business.entity.SportsTraining;
import com.dwxt.sport.business.entity.SportsTrainingDetails;
import com.dwxt.sport.business.vo.sportsTraining.InsertOrUpdateSportsTrainingDetailsVo;
import com.dwxt.sport.business.vo.sportsTraining.InsertOrUpdateSportsTrainingVo;
import com.dwxt.sport.business.vo.sportsTraining.SelectSportsTrainingVo;
import com.dwxt.sport.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 体育培训
 *
 * @author Kuang.JiaZhuo
 * @date 2020-07-31 10:41
 */
@Service
public class SportsTrainingService {

    @Resource
    SportsTrainingMapper sportsTrainingMapper;

    @Resource
    SportsTrainingDetailsMapper detailsMapper;

    /**
     * @方法名：insertOrUpdateSportsTraining
     * @描述： 新增或修改体育培训
     * @作者： kjz
     * @日期： Created in 2020/7/31 14:09
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateSportsTraining(InsertOrUpdateSportsTrainingVo insertOrUpdateVo) {
        String id = insertOrUpdateVo.getId();

        SportsTraining sportsTraining = new SportsTraining()
                .setName(insertOrUpdateVo.getName()) //设备名称
                .setRecommendedCoverage(insertOrUpdateVo.getRecommendedCoverage()); //建议覆盖率

        if(null==id || id.trim().length()==0){
            //新增
            String streetId = insertOrUpdateVo.getStreetId();
            if(null==streetId || streetId.trim().length()==0){
                throw new CustomException(StatusCode.STREET_ID_IS_NULL);
            }

            sportsTraining.setId(SnowIdUtil.getInstance().nextId())
                    .setCreateTime(LocalDateTime.now()).setIsDelete(VariableConstants.STRING_CONSTANT_0)
                    .setStreetId(streetId); //街道id
            return sportsTrainingMapper.insert(sportsTraining);
        }else{
            //更新
            sportsTraining.setUpdateTime(LocalDateTime.now()).setId(id);
            return sportsTrainingMapper.updateByPrimaryKeySelective(sportsTraining);
        }
    }

    /**
     * @方法名：selectSportsTrainingByCriteria
     * @描述： 按条件查询体育培训列表(带分页)
     * @作者： kjz
     * @日期： Created in 2020/7/31 14:10
     */
    public PageInfo<SportsTraining> selectSportsTrainingByCriteria(SelectSportsTrainingVo selectVo) {
        PageHelper.startPage(selectVo.getPageNum(), selectVo.getPageSize());
        List<SportsTraining> list = sportsTrainingMapper.selectSportsTrainingByCriteria(selectVo);
        PageInfo<SportsTraining> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * @方法名：deleteSportsTrainingByIds
     * @描述： 根据ids批量删除体育培训
     * @作者： kjz
     * @日期： Created in 2020/7/31 14:10
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteSportsTrainingByIds(String ids) {
        if(ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        String[] idArray = ids.split(",");

        return this.sportsTrainingMapper.deleteSportsTrainingByIds(idArray);
    }

    /**
     * @方法名：insertOrUpdateSportsTrainingDetails
     * @描述： 新增 或 更新 体育培训详情
     * @作者： kjz
     * @日期： Created in 2020/7/31 14:11
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateSportsTrainingDetails(InsertOrUpdateSportsTrainingDetailsVo insertOrUpdateVo) {

        String id = insertOrUpdateVo.getId();
        SportsTrainingDetails sportsTrainingDetails = new SportsTrainingDetails()
                .setOrganName(insertOrUpdateVo.getOrganName()) //机构名字
                .setClassroomCount(insertOrUpdateVo.getClassroomCount()) //教室数量
                .setTeacherCount(insertOrUpdateVo.getTeacherCount()) //老师数量
                .setStudentCount(insertOrUpdateVo.getStudentCount()) //学生数量
                .setTotalHistoryStudents(insertOrUpdateVo.getTotalHistoryStudents()) //历史学生总数
                .setMonitoringCount(insertOrUpdateVo.getMonitoringCount()) //接入监控设备数量
                .setDeviceStates(insertOrUpdateVo.getDeviceStates()); //设备状态

        String sportsTrainingId = insertOrUpdateVo.getSportsTrainingId();
        int result = 0;
        if (null == id || id.trim().length() == 0) {
            //新增

            if (null == sportsTrainingId || sportsTrainingId.trim().length() == 0) {
                throw new CustomException(StatusCode.SPORTS_TRAINING_ID_IS_NULL);
            }
            sportsTrainingDetails.setId(SnowIdUtil.getInstance().nextId())
                    .setCreateTime(LocalDateTime.now()).setIsDelete(VariableConstants.STRING_CONSTANT_0)
                    .setSportsTrainingId(sportsTrainingId); //体育培训关联id

            result = detailsMapper.insert(sportsTrainingDetails);

        } else {
            //更新
            sportsTrainingDetails.setUpdateTime(LocalDateTime.now()).setId(id);
            result = detailsMapper.updateByPrimaryKeySelective(sportsTrainingDetails);
        }

        if(result>0){
            this.updateSportsTraining(sportsTrainingId);
        }

        return result;
    }

    /**
     * @方法名：updateSportsTraining
     * @描述： 新增体育培训详情之后 更新 体育培训需要计算的数据
     * @作者： kjz
     * @日期： Created in 2020/8/5 11:06
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateSportsTraining(String sportsTrainingId) {
        HashMap map = detailsMapper.selectStatisticsBySportsTrainingId(sportsTrainingId);

        SportsTraining sportsTraining = new SportsTraining()
                .setOrganCount(Integer.valueOf(map.get("organCount").toString()))
                .setClassroomCount(Integer.valueOf(map.get("classroomCount").toString()))
                .setTeacherCount(Integer.valueOf(map.get("teacherCount").toString()))
                .setStudentCount(Integer.valueOf(map.get("studentCount").toString()))
                .setTotalHistoryStudents(Integer.valueOf(map.get("totalHistoryStudents").toString()))
                .setUpdateTime(LocalDateTime.now())
                .setId(sportsTrainingId);
        return sportsTrainingMapper.updateByPrimaryKeySelective(sportsTraining);
    }

    /**
     * @方法名：deleteSportsTrainingDetailsByIds
     * @描述： 根据ids批量删除 体育培训详情
     * @作者： kjz
     * @日期： Created in 2020/7/31 14:11
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteSportsTrainingDetailsByIds(String ids) {
        if(ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        String[] idArray = ids.split(",");
        int result = this.detailsMapper.deleteSportsTrainingDetailsByIds(idArray);
        if(result>0){
            String id = idArray[0];
            String sportsTrainingId = detailsMapper.selectByPrimaryKey(id).getSportsTrainingId();
            this.updateSportsTraining(sportsTrainingId);
        }
        return result;
    }

    /**
     * @方法名：selectSportsTrainingDetailsByPage
     * @描述： 根据【体育培训】的id查询详情(详情列表带分页)
     * @作者： kjz
     * @日期： Created in 2020/7/31 14:12
     */
    public PageInfo<SportsTrainingDetails> selectSportsTrainingDetailsByPage(Integer pageNum, Integer pageSize, String sportsTrainingId) {
        PageHelper.startPage(pageNum==null?1:pageNum, pageSize==null?10:pageSize);
        List<SportsTrainingDetails> list = detailsMapper.selectSportsTrainingDetailsBySportsTrainingId(sportsTrainingId);
        PageInfo<SportsTrainingDetails> pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
